package net.event.data_processing.utils;

import org.apache.commons.codec.Charsets;
import org.apache.jena.ext.com.google.common.io.Files;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtils {
    public static List<String> listDir(String dirPath){
        File[] files=new File(dirPath).listFiles();
        List<String> filePaths = new ArrayList<>();
        for(File file:files) {
            filePaths.add(file.getAbsolutePath());
        }
        return filePaths;
    }

    public static List<String> listDir(String dirPath, String suffix){
        File[] files=new File(dirPath).listFiles();
        List<String> filePaths = new ArrayList<>();
        for(File file:files) {
            String path = file.getAbsolutePath();
            if(path.toLowerCase().endsWith(suffix.toLowerCase()))
                filePaths.add(file.getAbsolutePath());
        }
        return filePaths;
    }
    
    public static String formatFileName(String oldFileName){
        return oldFileName.replaceAll("[^a-zA-Z0-9._]","_");
    }

    public static Map<String,String> loadRules(String filePath){
        Map<String, String> ruleMap = new HashMap<>(); 
        try{
            List<String> lines = Files.readLines(new File(filePath), Charsets.UTF_8);
            String ruleTmp = "";
            String ruleName = "";
            boolean readName = false;
            for (String line:lines) {
                if(line.startsWith("#")){
                    if(!readName){
                        if(!ruleTmp.replaceAll("\\s","").isEmpty()){
                            while (ruleMap.put(ruleName,ruleTmp) != null){
                                ruleName += "_1";
                                System.out.println("repeat name:"+ruleName);
//                                System.out.println("repeat value:"+ruleTmp);
                            }
                        }
                        ruleName = line;
                        ruleTmp = "";
                    }
                    readName = true;
                }else{
                    ruleTmp += line + "\n";
                    readName = false;
                }
            }
            ruleMap.put(ruleName,ruleTmp);
        }catch (Exception e){
            e.printStackTrace();
        }
        
        return ruleMap;
    }

    public static void main(String[] args) {
        System.out.println(1);
        Map<String, String> sparqls1 = FileUtils.loadRules("data/source/framebase_rules/dbpedia-pf-rules.txt");
        System.out.println(2);
        Map<String, String> sparqls2 = FileUtils.loadRules("data/source/framebase_rules/dbpediaEventsIntegrationRules.txt");
        System.out.println(3);
        Map<String, String> sparqls3 = FileUtils.loadRules("data/source/framebase_rules/freebaseSparqlIntegrationCFRules.txt");
        System.out.println(4);
        Map<String, String> sparqls5 = FileUtils.loadRules("data/source/framebase_rules/wikidata-pf-rules.txt");
        System.out.println(5);
        Map<String, String> sparqls6 = FileUtils.loadRules("data/source/framebase_rules/yagoIntegrationRulesSparql.txt");
    }
}
